﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;

namespace KnowlegeAssistant
{
    static class UserUtil
    {
        public static bool CheckLogin(string username, string password)
        {
            DataSet data = DatabaseConnect.execSP("CheckLogin", new List<string>() { username, password });

            if (data.Tables.Count != 0)
            {
                return true;
            }
            return false;
        }

        public static User GetUser(string username, string userpass)
        {
            User user = null;
            Role role = null;
            DataSet data = DatabaseConnect.execSP("GetUserByNameAndPass", new List<string>() { username, userpass });

            role = new Role(
                int.Parse(data.Tables[0].Rows[0]["role_id"].ToString()),
                data.Tables[0].Rows[0]["role_name"].ToString()
                );

            DataTable permissions = DatabaseConnect.execSP("GetPermissionsByRole", new List<string>() { role.RoleID.ToString() }).Tables[0];
            Permission permission = null;
            foreach (DataRow row in permissions.Rows)
            {
                permission = new Permission
                            (
                                int.Parse(row["permission_id"].ToString()),
                                row["description"].ToString(),
                                row["permission_name"].ToString()
                            );
                role.Permissions.Add(permission);
            }

            object owner = null;
            int owner_id = (int)data.Tables[0].Rows[0]["owner_id"];
            int owner_type_id = int.Parse(data.Tables[0].Rows[0]["owner_type_id"].ToString());
            switch (owner_type_id)
            {
                case 1: owner = StudentUtil.GetStudent(owner_id); break;
                case 2: owner = TeacherUtil.GetTeacher(owner_id); break;
                default: owner = null; break;
            }

            user = new User(
                int.Parse(data.Tables[0].Rows[0]["user_id"].ToString()),
                data.Tables[0].Rows[0]["user_name"].ToString(),
                data.Tables[0].Rows[0]["user_pass"].ToString(),
                role,
                owner
                );

            return user;
        }

        public static DataTable GetAllUsers(bool show_deleted)
        {
            DataSet data = DatabaseConnect.execSP("GetUsers 0," + ((show_deleted) ? "1" : "0"));
            return data.Tables[0];
        }

        public static DataTable GetUsersBySearchString(string searchString)
        {
            searchString = searchString.Trim();
            searchString = searchString.Length > 50
                          ? searchString.Substring(0, 50)
                          : searchString;

            if (searchString.Length != 0)
            {
                List<string> parameters = new List<string>();
                parameters.Add(searchString);
                return DatabaseConnect.execSP("GetUsersBySearchString", parameters).Tables[0];
            }
            else
            {
                return GetAllUsers(false);
            }
        }

        public static bool Exists(string username)
        {
            DataTable table = new DataTable();
            List<string> parameters = new List<string>() { username };
            table = DatabaseConnect.execSP("UserExists", parameters).Tables[0];
            return table.Rows.Count > 0;
        }

        public static void AddUser(string name, string password, int role_id, int owner_id, int owner_type_id)
        {
            List<string> parameters = new List<string>() { name, password, role_id.ToString(), owner_id.ToString(), owner_type_id.ToString() };
            DatabaseConnect.execSPNonQuery("AddUser", parameters);
        }

        public static void EditUser(string name, string password, string rolename)
        {
            if (Exists(name))
            {
                List<string> parameters = new List<string>() { name, password, rolename };
                DatabaseConnect.execSPNonQuery("EditUser", parameters);
            }
        }

        public static void DeleteUser(int user_id)
        {
            List<string> parameters = new List<string>() { user_id.ToString() };
            DatabaseConnect.execSPNonQuery("DeleteUser", parameters);
        }

        public static User GetUser(int user_id)
        {
            User user = null;
            DataTable table = DatabaseConnect.execSP("GetUserByID", new List<string>() { user_id.ToString() }).Tables[0];
            user = GetUser(table.Rows[0]["user_name"].ToString(), table.Rows[0]["user_pass"].ToString());
           
            return user;
        }
    }
}
